<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  

  
  <title>苟有恒 何必三更起五更眠 最无益 莫过一日曝十日寒</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta property="og:type" content="website">
<meta property="og:title" content="苟有恒 何必三更起五更眠 最无益 莫过一日曝十日寒">
<meta property="og:url" content="http://dontworryneedmoney.gitee.io/guolianbo/page/2/index.html">
<meta property="og:site_name" content="苟有恒 何必三更起五更眠 最无益 莫过一日曝十日寒">
<meta property="og:locale" content="en_US">
<meta property="article:author" content="worry dont">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/guolianbo/atom.xml" title="苟有恒 何必三更起五更眠 最无益 莫过一日曝十日寒" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  
    <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  
  
<link rel="stylesheet" href="/guolianbo/css/style.css">

<meta name="generator" content="Hexo 4.2.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/guolianbo/" id="logo">苟有恒 何必三更起五更眠 最无益 莫过一日曝十日寒</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/guolianbo/">Home</a>
        
          <a class="main-nav-link" href="/guolianbo/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/guolianbo/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://dontworryneedmoney.gitee.io/guolianbo"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main">
  
    <article id="ejs-dontworry/Algorithm/对称二叉树" class="article article-type-ejs" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/guolianbo/2020/04/30/dontworry/Algorithm/%E5%AF%B9%E7%A7%B0%E4%BA%8C%E5%8F%89%E6%A0%91/" class="article-date">
  <time datetime="2020-04-30T07:22:48.041Z" itemprop="datePublished">2020-04-30</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/guolianbo/2020/04/30/dontworry/Algorithm/%E5%AF%B9%E7%A7%B0%E4%BA%8C%E5%8F%89%E6%A0%91/">对称二叉树</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h2><p>请实现一个函数，用来判断一颗二叉树是不是对称的。注意，如果一个二叉树同此二叉树的镜像是同样的，定义其为对称的。</p>
<h2 id="错误代码"><a href="#错误代码" class="headerlink" title="错误代码"></a>错误代码</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> tree0 = <span class="keyword">new</span> TreeNode(<span class="number">1</span>);</span><br><span class="line"><span class="keyword">const</span> tree1 = <span class="keyword">new</span> TreeNode(<span class="number">2</span>);</span><br><span class="line"><span class="keyword">const</span> tree2 = <span class="keyword">new</span> TreeNode(<span class="number">2</span>);</span><br><span class="line"><span class="keyword">const</span> tree3 = <span class="keyword">new</span> TreeNode(<span class="number">3</span>);</span><br><span class="line"><span class="keyword">const</span> tree4 = <span class="keyword">new</span> TreeNode(<span class="number">4</span>);</span><br><span class="line"><span class="keyword">const</span> tree5 = <span class="keyword">new</span> TreeNode(<span class="number">3</span>);</span><br><span class="line"><span class="keyword">const</span> tree6 = <span class="keyword">new</span> TreeNode(<span class="number">4</span>);</span><br><span class="line"></span><br><span class="line">tree0.left = tree1;</span><br><span class="line">tree0.right = tree2;</span><br><span class="line">tree1.left = tree3;</span><br><span class="line">tree3.right = tree4;</span><br><span class="line">tree2.right = tree6;</span><br><span class="line">tree6.right = tree5;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(isSymmetrical(tree0));</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">TreeNode</span>(<span class="params">x</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">this</span>.val = x;</span><br><span class="line">  <span class="keyword">this</span>.left = <span class="literal">null</span>;</span><br><span class="line">  <span class="keyword">this</span>.right = <span class="literal">null</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">isSymmetrical</span>(<span class="params">pRoot</span>) </span>&#123;</span><br><span class="line">  <span class="comment">// write code here</span></span><br><span class="line">  <span class="keyword">if</span> (!pRoot) <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">  <span class="keyword">const</span> arr = middleSort(pRoot);</span><br><span class="line">  <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; arr.length / <span class="number">2</span>; i++) &#123;</span><br><span class="line">    <span class="keyword">if</span> (arr[i].val !== arr[arr.length - <span class="number">1</span> - i].val) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">middleSort</span>(<span class="params">pRoot</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">if</span> (!pRoot) <span class="keyword">return</span> [];</span><br><span class="line">  <span class="keyword">return</span> middleSort(pRoot.left).concat(pRoot).concat(middleSort(pRoot.right));</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="正确代码"><a href="#正确代码" class="headerlink" title="正确代码"></a>正确代码</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> tree0 = <span class="keyword">new</span> TreeNode(<span class="number">1</span>);</span><br><span class="line"><span class="keyword">const</span> tree1 = <span class="keyword">new</span> TreeNode(<span class="number">2</span>);</span><br><span class="line"><span class="keyword">const</span> tree2 = <span class="keyword">new</span> TreeNode(<span class="number">2</span>);</span><br><span class="line"><span class="keyword">const</span> tree3 = <span class="keyword">new</span> TreeNode(<span class="number">3</span>);</span><br><span class="line"><span class="keyword">const</span> tree4 = <span class="keyword">new</span> TreeNode(<span class="number">4</span>);</span><br><span class="line"><span class="keyword">const</span> tree5 = <span class="keyword">new</span> TreeNode(<span class="number">3</span>);</span><br><span class="line"><span class="keyword">const</span> tree6 = <span class="keyword">new</span> TreeNode(<span class="number">4</span>);</span><br><span class="line"></span><br><span class="line">tree0.left = tree1;</span><br><span class="line">tree0.right = tree2;</span><br><span class="line">tree1.left = tree3;</span><br><span class="line">tree3.right = tree4;</span><br><span class="line">tree2.right = tree5;</span><br><span class="line">tree5.left = tree6;</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(isSymmetrical(tree0));</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">TreeNode</span>(<span class="params">x</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">this</span>.val = x;</span><br><span class="line">  <span class="keyword">this</span>.left = <span class="literal">null</span>;</span><br><span class="line">  <span class="keyword">this</span>.right = <span class="literal">null</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">isSymmetrical</span>(<span class="params">pRoot</span>) </span>&#123;</span><br><span class="line">  <span class="comment">// write code here</span></span><br><span class="line">  <span class="keyword">if</span> (!pRoot) <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">  <span class="keyword">return</span> compare(pRoot.left, pRoot.right);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">compare</span>(<span class="params">left, right</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">if</span> (!left &amp;&amp; !right) <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">  <span class="keyword">if</span> (left &amp;&amp; right) &#123;</span><br><span class="line">    <span class="keyword">return</span> (</span><br><span class="line">      left.val === right.val &amp;&amp;</span><br><span class="line">      compare(left.left, right.right) &amp;&amp;</span><br><span class="line">      compare(left.right, right.left)</span><br><span class="line">    );</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>正确的代码中，主要是在根节点以下就开始分离的左右，进行比较，使用递归比较。最后保持结构和值都正确。</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://dontworryneedmoney.gitee.io/guolianbo/2020/04/30/dontworry/Algorithm/%E5%AF%B9%E7%A7%B0%E4%BA%8C%E5%8F%89%E6%A0%91/" data-id="ckb6hw6qz0008jcn24zd81mbi" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/guolianbo/tags/Algorithm/" rel="tag">Algorithm</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="ejs-dontworry/Algorithm/按层打印二叉树" class="article article-type-ejs" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/guolianbo/2020/04/29/dontworry/Algorithm/%E6%8C%89%E5%B1%82%E6%89%93%E5%8D%B0%E4%BA%8C%E5%8F%89%E6%A0%91/" class="article-date">
  <time datetime="2020-04-29T11:38:17.959Z" itemprop="datePublished">2020-04-29</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/guolianbo/2020/04/29/dontworry/Algorithm/%E6%8C%89%E5%B1%82%E6%89%93%E5%8D%B0%E4%BA%8C%E5%8F%89%E6%A0%91/">按层打印二叉树</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h2><p>从上到下按层打印二叉树，同一层结点从左至右输出。每一层输出一行。</p>
<h2 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> tree0 = <span class="keyword">new</span> TreeNode(<span class="number">8</span>);</span><br><span class="line"><span class="keyword">const</span> tree1 = <span class="keyword">new</span> TreeNode(<span class="number">6</span>);</span><br><span class="line"><span class="keyword">const</span> tree2 = <span class="keyword">new</span> TreeNode(<span class="number">10</span>);</span><br><span class="line"><span class="keyword">const</span> tree3 = <span class="keyword">new</span> TreeNode(<span class="number">5</span>);</span><br><span class="line"><span class="keyword">const</span> tree4 = <span class="keyword">new</span> TreeNode(<span class="number">7</span>);</span><br><span class="line"><span class="keyword">const</span> tree5 = <span class="keyword">new</span> TreeNode(<span class="number">9</span>);</span><br><span class="line"><span class="keyword">const</span> tree6 = <span class="keyword">new</span> TreeNode(<span class="number">11</span>);</span><br><span class="line"><span class="keyword">const</span> tree7 = <span class="keyword">new</span> TreeNode(<span class="number">1123</span>);</span><br><span class="line"><span class="keyword">const</span> tree8 = <span class="keyword">new</span> TreeNode(<span class="number">213</span>);</span><br><span class="line">tree0.left = tree1;</span><br><span class="line">tree0.right = tree2;</span><br><span class="line">tree1.left = tree3;</span><br><span class="line">tree1.right = tree4;</span><br><span class="line">tree2.left = tree5;</span><br><span class="line">tree2.right = tree6;</span><br><span class="line">tree5.right = tree7;</span><br><span class="line">tree7.left = tree8;</span><br><span class="line"><span class="built_in">console</span>.log(Print(tree0));</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">TreeNode</span>(<span class="params">x</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.val = x;</span><br><span class="line">    <span class="keyword">this</span>.left = <span class="literal">null</span>;</span><br><span class="line">    <span class="keyword">this</span>.right = <span class="literal">null</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Print</span>(<span class="params">pRoot</span>) </span>&#123;</span><br><span class="line">    <span class="comment">// write code here</span></span><br><span class="line">    <span class="keyword">if</span> (!pRoot) <span class="keyword">return</span> [];</span><br><span class="line">    <span class="keyword">let</span> nodeArr = [];</span><br><span class="line">    <span class="keyword">const</span> queueTree = [];</span><br><span class="line">    queueTree.push(pRoot);</span><br><span class="line">    <span class="keyword">while</span> (queueTree.length &gt; <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="keyword">const</span> currentLength = queueTree.length;</span><br><span class="line">        <span class="keyword">const</span> tmpNodes = [];</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; currentLength; i++) &#123; <span class="comment">// 使用for循环，才能确定层，其他方法都不行。</span></span><br><span class="line">            <span class="keyword">const</span> node = queueTree.shift();</span><br><span class="line">            tmpNodes.push(node.val);</span><br><span class="line">            node.left &amp;&amp; queueTree.push(node.left);</span><br><span class="line">            node.right &amp;&amp; queueTree.push(node.right);</span><br><span class="line">        &#125;</span><br><span class="line">        nodeArr.push(tmpNodes);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> nodeArr;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>广度遍历分层，注意<strong>需要在while中使用for循环，输出整层的节点。</strong></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://dontworryneedmoney.gitee.io/guolianbo/2020/04/29/dontworry/Algorithm/%E6%8C%89%E5%B1%82%E6%89%93%E5%8D%B0%E4%BA%8C%E5%8F%89%E6%A0%91/" data-id="ckb6hw6r7000vjcn25mpncdb1" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/guolianbo/tags/Algorithm/" rel="tag">Algorithm</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="ejs-dontworry/Algorithm/序列化二叉树" class="article article-type-ejs" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/guolianbo/2020/04/29/dontworry/Algorithm/%E5%BA%8F%E5%88%97%E5%8C%96%E4%BA%8C%E5%8F%89%E6%A0%91/" class="article-date">
  <time datetime="2020-04-29T07:12:43.397Z" itemprop="datePublished">2020-04-29</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/guolianbo/2020/04/29/dontworry/Algorithm/%E5%BA%8F%E5%88%97%E5%8C%96%E4%BA%8C%E5%8F%89%E6%A0%91/">序列化二叉树</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h2><p>请实现两个函数，分别用来序列化和反序列化二叉树</p>
<p>二叉树的序列化是指：把一棵二叉树按照某种遍历方式的结果以某种格式保存为字符串，从而使得内存中建立起来的二叉树可以持久保存。序列化可以基于先序、中序、后序、层序的二叉树遍历方式来进行修改，序列化的结果是一个字符串，序列化时通过 某种符号表示空节点（#），以 ！ 表示一个结点值的结束（value!）。</p>
<p>二叉树的反序列化是指：根据某种遍历顺序得到的序列化字符串结果str，重构二叉树。</p>
<p>例如，我们可以把一个只有根节点为1的二叉树序列化为”1,”，然后通过自己的函数来解析回这个二叉树</p>
<h2 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> tree0 = <span class="keyword">new</span> TreeNode(<span class="number">8</span>);</span><br><span class="line"><span class="keyword">const</span> tree1 = <span class="keyword">new</span> TreeNode(<span class="number">6</span>);</span><br><span class="line"><span class="keyword">const</span> tree2 = <span class="keyword">new</span> TreeNode(<span class="number">10</span>);</span><br><span class="line"><span class="keyword">const</span> tree3 = <span class="keyword">new</span> TreeNode(<span class="number">5</span>);</span><br><span class="line"><span class="keyword">const</span> tree4 = <span class="keyword">new</span> TreeNode(<span class="number">7</span>);</span><br><span class="line"><span class="keyword">const</span> tree5 = <span class="keyword">new</span> TreeNode(<span class="number">9</span>);</span><br><span class="line"><span class="keyword">const</span> tree6 = <span class="keyword">new</span> TreeNode(<span class="number">11</span>);</span><br><span class="line">tree0.left = tree1;</span><br><span class="line">tree0.right = tree2;</span><br><span class="line">tree1.left = tree3;</span><br><span class="line">tree1.right = tree4;</span><br><span class="line">tree2.left = tree5;</span><br><span class="line">tree2.right = tree6;</span><br><span class="line"><span class="built_in">console</span>.log(Serialize(tree0));</span><br><span class="line"><span class="built_in">console</span>.log(Deserialize(Serialize(tree0)));</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">TreeNode</span>(<span class="params">x</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.val = x;</span><br><span class="line">    <span class="keyword">this</span>.left = <span class="literal">null</span>;</span><br><span class="line">    <span class="keyword">this</span>.right = <span class="literal">null</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Serialize</span>(<span class="params">pRoot</span>) </span>&#123;</span><br><span class="line">    <span class="comment">// write code here</span></span><br><span class="line">    <span class="keyword">if</span> (!pRoot) <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">    <span class="keyword">let</span> strArray = <span class="string">""</span>;</span><br><span class="line">    <span class="keyword">const</span> queueTree = [];</span><br><span class="line">    <span class="keyword">let</span> currentNode = <span class="literal">null</span>;</span><br><span class="line">    queueTree.push(pRoot);</span><br><span class="line">    <span class="keyword">while</span> (queueTree.length &gt; <span class="number">0</span>) &#123;</span><br><span class="line">        currentNode = queueTree.shift();</span><br><span class="line">        <span class="keyword">if</span>( currentNode)&#123;</span><br><span class="line">            strArray = strArray + currentNode.val + <span class="string">";"</span>;</span><br><span class="line">            queueTree.push(currentNode.left);</span><br><span class="line">            queueTree.push(currentNode.right);</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            strArray = strArray + <span class="string">'#'</span> + <span class="string">";"</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> strArray;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Deserialize</span>(<span class="params">s</span>) </span>&#123;</span><br><span class="line">    <span class="comment">// write code here</span></span><br><span class="line">    <span class="keyword">if</span> (!s) <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">    <span class="keyword">const</span> strArray = s.split(<span class="string">";"</span>);</span><br><span class="line">    <span class="keyword">let</span> root = <span class="literal">null</span>;</span><br><span class="line">    <span class="keyword">if</span> (strArray[<span class="number">0</span>]) &#123;</span><br><span class="line">        root = <span class="keyword">new</span> TreeNode(strArray[<span class="number">0</span>]);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (!root) <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">    <span class="keyword">const</span> nodeQueue = [];</span><br><span class="line">    nodeQueue.push(root);</span><br><span class="line">    <span class="keyword">let</span> index = <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">while</span> (nodeQueue.length &gt; <span class="number">0</span>) &#123;</span><br><span class="line">        <span class="keyword">const</span> currentNode = nodeQueue.shift();</span><br><span class="line">        <span class="keyword">if</span>(strArray[index] === <span class="string">'#'</span>)&#123;</span><br><span class="line">            currentNode.left =<span class="literal">null</span>;</span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">            currentNode.left = <span class="keyword">new</span> TreeNode(strArray[index]);</span><br><span class="line">            nodeQueue.push(currentNode.left);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span>(strArray[index + <span class="number">1</span>] === <span class="string">'#'</span>)&#123;</span><br><span class="line">            currentNode.right =<span class="literal">null</span>;</span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">            currentNode.right = <span class="keyword">new</span> TreeNode(strArray[index + <span class="number">1</span>]);</span><br><span class="line">            nodeQueue.push(currentNode.right);</span><br><span class="line">        &#125;</span><br><span class="line">        index += <span class="number">2</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> root;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>使用广度遍历进行序列化，还是挺简单的，只是会多一些<code>#</code>;</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://dontworryneedmoney.gitee.io/guolianbo/2020/04/29/dontworry/Algorithm/%E5%BA%8F%E5%88%97%E5%8C%96%E4%BA%8C%E5%8F%89%E6%A0%91/" data-id="ckb6hw6qv0001jcn207816i7c" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/guolianbo/tags/Algorithm/" rel="tag">Algorithm</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="ejs-dontworry/Algorithm/搜索二叉树的第k个节点" class="article article-type-ejs" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/guolianbo/2020/04/28/dontworry/Algorithm/%E6%90%9C%E7%B4%A2%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E7%AC%ACk%E4%B8%AA%E8%8A%82%E7%82%B9/" class="article-date">
  <time datetime="2020-04-28T06:39:02.674Z" itemprop="datePublished">2020-04-28</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/guolianbo/2020/04/28/dontworry/Algorithm/%E6%90%9C%E7%B4%A2%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E7%AC%ACk%E4%B8%AA%E8%8A%82%E7%82%B9/">搜索二叉树的第k个节点</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h2><p>给定一棵二叉搜索树，请找出其中的第k小的结点。例如， （5，3，7，2，4，6，8）    中，按结点数值大小顺序第三小结点的值为4。</p>
<h2 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> tree0 = <span class="keyword">new</span> TreeNode(<span class="number">8</span>);</span><br><span class="line"><span class="keyword">const</span> tree1 = <span class="keyword">new</span> TreeNode(<span class="number">6</span>);</span><br><span class="line"><span class="keyword">const</span> tree2 = <span class="keyword">new</span> TreeNode(<span class="number">10</span>);</span><br><span class="line"><span class="keyword">const</span> tree3 = <span class="keyword">new</span> TreeNode(<span class="number">5</span>);</span><br><span class="line"><span class="keyword">const</span> tree4 = <span class="keyword">new</span> TreeNode(<span class="number">7</span>);</span><br><span class="line"><span class="keyword">const</span> tree5 = <span class="keyword">new</span> TreeNode(<span class="number">9</span>);</span><br><span class="line"><span class="keyword">const</span> tree6 = <span class="keyword">new</span> TreeNode(<span class="number">11</span>);</span><br><span class="line">tree0.left = tree1;</span><br><span class="line">tree0.right = tree2;</span><br><span class="line">tree1.left = tree3;</span><br><span class="line">tree1.right = tree4;</span><br><span class="line">tree2.left = tree5;</span><br><span class="line">tree2.right = tree6;</span><br><span class="line"><span class="built_in">console</span>.log(KthNode(tree0, <span class="number">1</span>));</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">TreeNode</span>(<span class="params">x</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">this</span>.val = x;</span><br><span class="line">    <span class="keyword">this</span>.left = <span class="literal">null</span>;</span><br><span class="line">    <span class="keyword">this</span>.right = <span class="literal">null</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">KthNode</span>(<span class="params">pRoot, k</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (!pRoot) <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">    <span class="keyword">if</span> (!k) <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">    <span class="keyword">const</span> leftCount = calcNodeNum(pRoot.left);</span><br><span class="line">    <span class="keyword">const</span> rootIndex = leftCount + <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">if</span> (k &lt; rootIndex) &#123;</span><br><span class="line">        <span class="keyword">return</span> KthNode(pRoot.left, k);</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span> (k &gt; rootIndex) &#123;</span><br><span class="line">        <span class="keyword">return</span> KthNode(pRoot.right, k - rootIndex);</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> pRoot;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//  计算节点的总个数</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">calcNodeNum</span>(<span class="params">node</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (!node) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">1</span> + calcNodeNum(node.right) + calcNodeNum(node.left);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p>特别适合递归</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://dontworryneedmoney.gitee.io/guolianbo/2020/04/28/dontworry/Algorithm/%E6%90%9C%E7%B4%A2%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E7%AC%ACk%E4%B8%AA%E8%8A%82%E7%82%B9/" data-id="ckb6hw6qx0004jcn26un43ny5" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/guolianbo/tags/Algorithm/" rel="tag">Algorithm</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="ejs-dontworry/Algorithm/深度遍历广度遍历" class="article article-type-ejs" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/guolianbo/2020/04/28/dontworry/Algorithm/%E6%B7%B1%E5%BA%A6%E9%81%8D%E5%8E%86%E5%B9%BF%E5%BA%A6%E9%81%8D%E5%8E%86/" class="article-date">
  <time datetime="2020-04-28T03:02:50.644Z" itemprop="datePublished">2020-04-28</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/guolianbo/2020/04/28/dontworry/Algorithm/%E6%B7%B1%E5%BA%A6%E9%81%8D%E5%8E%86%E5%B9%BF%E5%BA%A6%E9%81%8D%E5%8E%86/">深度遍历广度遍历</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h1 id="深度遍历广度遍历"><a href="#深度遍历广度遍历" class="headerlink" title="深度遍历广度遍历"></a>深度遍历广度遍历</h1><figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span>  广度遍历</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;param name="tf"&gt;</span><span class="doctag">&lt;/param&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;returns&gt;</span><span class="doctag">&lt;/returns&gt;</span></span></span><br><span class="line">    <span class="function">List&lt;Transform&gt; <span class="title">FindChildRecursion1</span>(<span class="params">Transform tf</span>)</span></span><br><span class="line"><span class="function"></span>    &#123;</span><br><span class="line">        List&lt;Transform&gt; allTf = <span class="keyword">new</span> List&lt;Transform&gt;();</span><br><span class="line">        Queue&lt;Transform&gt; parent = <span class="keyword">new</span> Queue&lt;Transform&gt;();</span><br><span class="line">        <span class="keyword">if</span> (tf == <span class="literal">null</span>) <span class="keyword">return</span> allTf;</span><br><span class="line">        allTf.Add(tf);</span><br><span class="line">        <span class="keyword">if</span> (tf.childCount == <span class="number">0</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">return</span> allTf;</span><br><span class="line">        &#125;</span><br><span class="line">        parent.Enqueue(tf);</span><br><span class="line">        Transform currentTf;</span><br><span class="line">        <span class="keyword">while</span> (parent.Count != <span class="number">0</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            currentTf = parent.Dequeue();</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; currentTf.childCount; i++)</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">var</span> child = currentTf.GetChild(i);</span><br><span class="line">                allTf.Add(child);</span><br><span class="line">                <span class="keyword">if</span> (child.childCount != <span class="number">0</span>)</span><br><span class="line">                &#123;</span><br><span class="line">                    parent.Enqueue(child);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> allTf;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 递归深度遍历</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;param name="tf"&gt;</span><span class="doctag">&lt;/param&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;returns&gt;</span><span class="doctag">&lt;/returns&gt;</span></span></span><br><span class="line">    <span class="function">List&lt;Transform&gt; <span class="title">FindChildRecursion2</span>(<span class="params">Transform tf</span>)</span></span><br><span class="line"><span class="function"></span>    &#123;</span><br><span class="line">        List&lt;Transform&gt; allTf = <span class="keyword">new</span> List&lt;Transform&gt;();</span><br><span class="line">        <span class="keyword">if</span> (tf == <span class="literal">null</span>) <span class="keyword">return</span> allTf;</span><br><span class="line">        allTf.Add(tf);</span><br><span class="line">        <span class="keyword">if</span> (tf.childCount == <span class="number">0</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">return</span> allTf;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; tf.childCount; i++)</span><br><span class="line">        &#123;</span><br><span class="line">            allTf.AddRange(FindChildRecursion2(tf.GetChild(i)));</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> allTf;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> 非递归深度遍历</span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;param name="tf"&gt;</span><span class="doctag">&lt;/param&gt;</span></span></span><br><span class="line">    <span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;returns&gt;</span><span class="doctag">&lt;/returns&gt;</span></span></span><br><span class="line">    <span class="function">List&lt;Transform&gt; <span class="title">FindChildRecursion3</span>(<span class="params">Transform tf</span>)</span></span><br><span class="line"><span class="function"></span>    &#123;</span><br><span class="line">        List&lt;Transform&gt; allTf = <span class="keyword">new</span> List&lt;Transform&gt;();</span><br><span class="line">        <span class="keyword">if</span> (tf == <span class="literal">null</span>) <span class="keyword">return</span> allTf;</span><br><span class="line">        allTf.Add(tf);</span><br><span class="line">        <span class="keyword">if</span> (tf.childCount == <span class="number">0</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">return</span> allTf;</span><br><span class="line">        &#125;</span><br><span class="line">        Transform currenttf = tf;</span><br><span class="line">        Transform parnet = tf.parent;</span><br><span class="line">        <span class="keyword">int</span> childIndex = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">while</span> (currenttf != parnet)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">while</span> (childIndex &lt; currenttf.childCount)</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="keyword">var</span> tmpCurrenttf = currenttf.GetChild(childIndex);</span><br><span class="line">                allTf.Add(tmpCurrenttf);</span><br><span class="line">                <span class="keyword">if</span> (tmpCurrenttf.childCount != <span class="number">0</span>)</span><br><span class="line">                &#123;</span><br><span class="line">                    currenttf = tmpCurrenttf;</span><br><span class="line">                    childIndex = <span class="number">0</span>;</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">else</span></span><br><span class="line">                &#123;</span><br><span class="line">                    childIndex++;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line"></span><br><span class="line">            childIndex = currenttf.GetSiblingIndex() + <span class="number">1</span>;</span><br><span class="line">            currenttf = currenttf.parent;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> allTf;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://dontworryneedmoney.gitee.io/guolianbo/2020/04/28/dontworry/Algorithm/%E6%B7%B1%E5%BA%A6%E9%81%8D%E5%8E%86%E5%B9%BF%E5%BA%A6%E9%81%8D%E5%8E%86/" data-id="ckb6hw6qy0005jcn2gud3fwq9" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/guolianbo/tags/Algorithm/" rel="tag">Algorithm</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="ejs-dontworry/Algorithm/矩阵中的路径" class="article article-type-ejs" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/guolianbo/2020/04/27/dontworry/Algorithm/%E7%9F%A9%E9%98%B5%E4%B8%AD%E7%9A%84%E8%B7%AF%E5%BE%84/" class="article-date">
  <time datetime="2020-04-27T11:06:35.559Z" itemprop="datePublished">2020-04-27</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/guolianbo/2020/04/27/dontworry/Algorithm/%E7%9F%A9%E9%98%B5%E4%B8%AD%E7%9A%84%E8%B7%AF%E5%BE%84/">矩阵中的路径</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h2><p>请设计一个函数，用来判断在一个矩阵中是否存在一条包含某字符串所有字符的路径。路径可以从矩阵中的任意一个格子开始，每一步可以在矩阵中向左，向右，向上，向下移动一个格子。如果一条路径经过了矩阵中的某一个格子，则该路径不能再进入该格子。 例如 </p>

$$
 \left\{
 \begin{matrix}
   a & b & c & e \\
   s & f & c & s \\
   a & d & e & e \\
  \end{matrix}
  \right\}
  $$


<p>矩阵中包含一条字符串”bcced”的路径，但是矩阵中不包含”abcb”路径，因为字符串的第一个字符b占据了矩阵中的第一行第二个格子之后，路径不能再次进入该格子。</p>
<h2 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(hasPath(<span class="string">"ABCESFCSADEE"</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="string">"ABCB"</span>));</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">hasPath</span>(<span class="params">matrix, rows, cols, path</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">const</span> stringMat = twoArray(matrix, rows, cols);</span><br><span class="line">    <span class="keyword">const</span> pathArray = path.split(<span class="string">''</span>);</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> row = <span class="number">0</span>; row &lt; stringMat.length; row++) &#123;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">let</span> col = <span class="number">0</span>; col &lt; stringMat[row].length; col++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (pathArray[<span class="number">0</span>] === stringMat[row][col])&#123;</span><br><span class="line">                <span class="keyword">const</span> visitedMat = visited(rows,cols);</span><br><span class="line">                <span class="keyword">if</span> (move(row, col, rows, cols, stringMat, visitedMat, <span class="number">0</span>, pathArray)) &#123;</span><br><span class="line">                    <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">move</span>(<span class="params">i, j, rows, cols, stringMat, visitedMat, index, pathArray</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (i &lt; <span class="number">0</span> || j &lt; <span class="number">0</span> || i &gt;= rows || j &gt; cols) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    <span class="keyword">if</span> (visitedMat[i][j]) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    <span class="keyword">if</span> (stringMat[i][j] !== pathArray[index]) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    visitedMat[i][j] = <span class="literal">true</span>;</span><br><span class="line">    <span class="keyword">if</span>(index === pathArray.length <span class="number">-1</span> ) <span class="keyword">return</span> <span class="literal">true</span>; </span><br><span class="line">    <span class="keyword">return</span> move(i + <span class="number">1</span>, j, rows, cols, stringMat, visitedMat, index + <span class="number">1</span>, pathArray)</span><br><span class="line">        || move(i - <span class="number">1</span>, j, rows, cols, stringMat, visitedMat, index + <span class="number">1</span>, pathArray)</span><br><span class="line">        || move(i, j - <span class="number">1</span>, rows, cols, stringMat, visitedMat, index + <span class="number">1</span>, pathArray)</span><br><span class="line">        || move(i, j + <span class="number">1</span>, rows, cols, stringMat, visitedMat, index + <span class="number">1</span>, pathArray);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">twoArray</span>(<span class="params">matrix, rows, cols</span>) </span>&#123; <span class="comment">// rows 行数  cols 列数</span></span><br><span class="line">    <span class="keyword">const</span> stringArray = matrix.split(<span class="string">''</span>);</span><br><span class="line">    <span class="keyword">let</span> matrixArrayOfTwo = <span class="keyword">new</span> <span class="built_in">Array</span>();</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; rows; i++) &#123;</span><br><span class="line">        matrixArrayOfTwo[i] =  <span class="keyword">new</span> <span class="built_in">Array</span>();</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">let</span> j = <span class="number">0</span>; j &lt; cols; j++) &#123;</span><br><span class="line">            matrixArrayOfTwo[i][j] = stringArray[i * cols + j];</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> matrixArrayOfTwo;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">visited</span>(<span class="params">rows, cols</span>) </span>&#123; <span class="comment">// rows 行数  cols 列数</span></span><br><span class="line">    <span class="keyword">let</span> visitedMat =  <span class="keyword">new</span> <span class="built_in">Array</span>();</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">let</span> i = <span class="number">0</span>; i &lt; rows; i++) &#123;</span><br><span class="line">        visitedMat[i] =  <span class="keyword">new</span> <span class="built_in">Array</span>();</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">let</span> j = <span class="number">0</span>; j &lt; cols; j++) &#123;</span><br><span class="line">            visitedMat[i][j] = <span class="literal">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> visitedMat;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><ul>
<li>数组越界的问题永远是问题，<code>if(index === pathArray.length -1 ) return true;</code>当遍历完成后，即已经成功了。所有使用到数组的地方，越界永远是最先需要考虑的</li>
<li>已访问的位置，在每次开始时需要重置<code>const visitedMat = visited(rows,cols);</code>，涉及到循环的代码，一定要了解每个变量的生命周期和流程。不要出现以前循环的内容影响到下次循环的结果的操作。</li>
</ul>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://dontworryneedmoney.gitee.io/guolianbo/2020/04/27/dontworry/Algorithm/%E7%9F%A9%E9%98%B5%E4%B8%AD%E7%9A%84%E8%B7%AF%E5%BE%84/" data-id="ckb6hw6r2000hjcn2htcneote" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/guolianbo/tags/Algorithm/" rel="tag">Algorithm</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="ejs-dontworry/Algorithm/非递减旋转数组找最小值" class="article article-type-ejs" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/guolianbo/2020/04/27/dontworry/Algorithm/%E9%9D%9E%E9%80%92%E5%87%8F%E6%97%8B%E8%BD%AC%E6%95%B0%E7%BB%84%E6%89%BE%E6%9C%80%E5%B0%8F%E5%80%BC/" class="article-date">
  <time datetime="2020-04-27T09:21:56.383Z" itemprop="datePublished">2020-04-27</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/guolianbo/2020/04/27/dontworry/Algorithm/%E9%9D%9E%E9%80%92%E5%87%8F%E6%97%8B%E8%BD%AC%E6%95%B0%E7%BB%84%E6%89%BE%E6%9C%80%E5%B0%8F%E5%80%BC/">非递减旋转数组找最小值</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <!-- TOC -->

<!-- /TOC -->
<h2 id="描述"><a href="#描述" class="headerlink" title="描述"></a>描述</h2><p>把一个数组最开始的若干个元素搬到数组的末尾，我们称之为数组的旋转。<br>输入一个非递减排序的数组的一个旋转，输出旋转数组的最小元素。<br>例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转，该数组的最小值为1。<br>NOTE：给出的所有元素都大于0，若数组大小为0，请返回0。</p>
<h2 id="循环算法"><a href="#循环算法" class="headerlink" title="循环算法"></a>循环算法</h2><figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">minNumberInRotateArray</span>(<span class="params"><span class="keyword">int</span>[] array</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(array.Length ==<span class="number">0</span>) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;array.Length <span class="number">-1</span>;i++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">if</span>(array[i]&gt;array[i+<span class="number">1</span>])&#123;</span><br><span class="line">            <span class="keyword">return</span> array[i+<span class="number">1</span>];</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> array[<span class="number">0</span>];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="二分法"><a href="#二分法" class="headerlink" title="二分法"></a>二分法</h2><figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">minNumberInRotateArray</span>(<span class="params"><span class="keyword">int</span>[] array</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> low = <span class="number">0</span> ; <span class="keyword">int</span> high = array.Length - <span class="number">1</span>;   </span><br><span class="line">    <span class="keyword">while</span>(low &lt; high)&#123;</span><br><span class="line">        <span class="keyword">int</span> mid = low + (high - low) / <span class="number">2</span>;        </span><br><span class="line">        <span class="keyword">if</span>(array[mid] &gt; array[high])&#123;</span><br><span class="line">            low = mid + <span class="number">1</span>;</span><br><span class="line">        &#125;<span class="keyword">else</span> <span class="keyword">if</span>(array[mid] == array[high])&#123;</span><br><span class="line">            high = high - <span class="number">1</span>;</span><br><span class="line">        &#125;<span class="keyword">else</span>&#123;</span><br><span class="line">            high = mid;</span><br><span class="line">        &#125;   </span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> array[low];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://dontworryneedmoney.gitee.io/guolianbo/2020/04/27/dontworry/Algorithm/%E9%9D%9E%E9%80%92%E5%87%8F%E6%97%8B%E8%BD%AC%E6%95%B0%E7%BB%84%E6%89%BE%E6%9C%80%E5%B0%8F%E5%80%BC/" data-id="ckb6hw6r6000tjcn2917y0ujg" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/guolianbo/tags/Algorithm/" rel="tag">Algorithm</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="ejs-dontworry/Algorithm/跳格子" class="article article-type-ejs" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/guolianbo/2020/04/27/dontworry/Algorithm/%E8%B7%B3%E6%A0%BC%E5%AD%90/" class="article-date">
  <time datetime="2020-04-26T16:20:17.000Z" itemprop="datePublished">2020-04-27</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/guolianbo/2020/04/27/dontworry/Algorithm/%E8%B7%B3%E6%A0%BC%E5%AD%90/">跳格子</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h2><p>地上有一个m行和n列的方格。一个机器人从坐标0,0的格子开始移动，每一次只能向左，右，上，下四个方向移动一格，但是不能进入行坐标和列坐标的数位之和大于k的格子。 例如，当k为18时，机器人能够进入方格（35,37），因为3+5+3+7 = 18。但是，它不能进入方格（35,38），因为3+5+3+8 = 19。请问该机器人能够达到多少个格子？</p>
<h2 id="递归"><a href="#递归" class="headerlink" title="递归"></a>递归</h2><figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;summary&gt;</span></span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> </span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;/summary&gt;</span></span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;param name="threshold"&gt;</span><span class="doctag">&lt;/param&gt;</span></span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;param name="rows"&gt;</span>行数<span class="doctag">&lt;/param&gt;</span></span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;param name="cols"&gt;</span>列数<span class="doctag">&lt;/param&gt;</span></span></span><br><span class="line"><span class="comment"><span class="doctag">///</span> <span class="doctag">&lt;returns&gt;</span><span class="doctag">&lt;/returns&gt;</span></span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">movingCount</span>(<span class="params"><span class="keyword">int</span> threshold, <span class="keyword">int</span> rows, <span class="keyword">int</span> cols</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">bool</span>[,] visited = <span class="keyword">new</span> <span class="keyword">bool</span>[cols, rows]; <span class="comment">// cols 一行有cols个，一列有rows个</span></span><br><span class="line">    <span class="keyword">return</span> movingCountCore(threshold, rows, cols, <span class="number">0</span>, <span class="number">0</span>, visited);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">movingCountCore</span>(<span class="params"><span class="keyword">int</span> threshold, <span class="keyword">int</span> rows, <span class="keyword">int</span> cols, <span class="keyword">int</span> i, <span class="keyword">int</span> j, <span class="keyword">bool</span>[,] visited</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (i &lt; <span class="number">0</span> || j &lt; <span class="number">0</span> || i &gt;= cols || j &gt;= rows) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">if</span> (!checkSum(threshold, i, j)) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">if</span> (visited[i, j]) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">    visited[i, j] = <span class="literal">true</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">1</span> + movingCountCore(threshold, rows, cols, i + <span class="number">1</span>, j, visited)</span><br><span class="line">             + movingCountCore(threshold, rows, cols, i - <span class="number">1</span>, j, visited)</span><br><span class="line">             + movingCountCore(threshold, rows, cols, i, j + <span class="number">1</span>, visited)</span><br><span class="line">             + movingCountCore(threshold, rows, cols, i, j - <span class="number">1</span>, visited);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">checkSum</span>(<span class="params"><span class="keyword">int</span> threshold, <span class="keyword">int</span> i, <span class="keyword">int</span> j</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> sum = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">while</span> (i &gt; <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        sum += i % <span class="number">10</span>;</span><br><span class="line">        i = i / <span class="number">10</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span> (j &gt; <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        sum += j % <span class="number">10</span>;</span><br><span class="line">        j = j / <span class="number">10</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> (sum &lt;= threshold) <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="注意事项"><a href="#注意事项" class="headerlink" title="注意事项"></a>注意事项</h2><p>递归的思想很简单，感觉性能上不太好。还有就是<code>bool[,] visited = new bool[cols, rows]</code>cols是表示的二维数组的一行的个数，rows表示的一列的个数，不要和行数和一个行的个数弄混了，比如建一个5行4列的数组，应该是<code>new int[4,5]</code>，而不是直观的<code>new int [5,4]</code></p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://dontworryneedmoney.gitee.io/guolianbo/2020/04/27/dontworry/Algorithm/%E8%B7%B3%E6%A0%BC%E5%AD%90/" data-id="ckb6hw6r3000jjcn2f8xg00nn" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/guolianbo/tags/%E6%9D%82%E9%A1%B9/" rel="tag">杂项</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="ejs-dontworry/Algorithm/剪绳子算法" class="article article-type-ejs" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/guolianbo/2020/04/25/dontworry/Algorithm/%E5%89%AA%E7%BB%B3%E5%AD%90%E7%AE%97%E6%B3%95/" class="article-date">
  <time datetime="2020-04-25T06:04:17.000Z" itemprop="datePublished">2020-04-25</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/guolianbo/2020/04/25/dontworry/Algorithm/%E5%89%AA%E7%BB%B3%E5%AD%90%E7%AE%97%E6%B3%95/">剪绳子算法</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p><a href="#介绍">介绍</a><br><a href="#动态规划代码（我也不知道啥叫动态规划）">动态规划代码（我也不知道啥叫动态规划）</a></p>
<h2 id="介绍"><a href="#介绍" class="headerlink" title="介绍"></a>介绍</h2><p>给你一根长度为n的绳子，请把绳子剪成整数长的m段（m、n都是整数，n&gt;1并且m&gt;1），每段绳子的长度记为k[0],k[1],…,k[m]。请问k[0]xk[1]x…xk[m]可能的最大乘积是多少？例如，当绳子的长度是8时，我们把它剪成长度分别为2、3、3的三段，此时得到的最大乘积是18。</p>
<h2 id="动态规划代码（我也不知道啥叫动态规划）"><a href="#动态规划代码（我也不知道啥叫动态规划）" class="headerlink" title="动态规划代码（我也不知道啥叫动态规划）"></a>动态规划代码（我也不知道啥叫动态规划）</h2><figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">CutRope</span>(<span class="params"><span class="keyword">int</span> number</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (number &lt;= <span class="number">0</span>) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">if</span> (number == <span class="number">1</span>) <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">if</span> (number == <span class="number">2</span>) <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">if</span> (number == <span class="number">3</span>) <span class="keyword">return</span> <span class="number">2</span>; <span class="comment">// 返回的值</span></span><br><span class="line">    <span class="keyword">int</span>[] maxArray = <span class="keyword">new</span> <span class="keyword">int</span>[number + <span class="number">1</span>];</span><br><span class="line">    maxArray[<span class="number">1</span>] = <span class="number">1</span>;</span><br><span class="line">    maxArray[<span class="number">2</span>] = <span class="number">2</span>;</span><br><span class="line">    maxArray[<span class="number">3</span>] = <span class="number">3</span>; <span class="comment">// 乘积最大值</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">4</span>; i &lt; maxArray.Length; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">int</span> maxProduct = <span class="number">0</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">1</span>; j &lt; i / <span class="number">2</span>; j++)</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="keyword">int</span> productResult = maxArray[j] * maxArray[i - j]; <span class="comment">// 以前的乘积最大值现在相乘也一定最大</span></span><br><span class="line">            <span class="keyword">if</span> (productResult &gt; maxProduct)</span><br><span class="line">            &#123;</span><br><span class="line">                maxProduct = productResult;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        maxArray[i] = maxProduct;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> maxArray[number];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="使用数学原理求解"><a href="#使用数学原理求解" class="headerlink" title="使用数学原理求解"></a>使用数学原理求解</h2><figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">cutRope</span>(<span class="params"><span class="keyword">int</span> number</span>)</span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (number &lt;= <span class="number">0</span>) <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">if</span> (number == <span class="number">1</span>) <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">if</span> (number == <span class="number">2</span>) <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">if</span> (number == <span class="number">3</span>) <span class="keyword">return</span> <span class="number">2</span>; <span class="comment">// 返回的值</span></span><br><span class="line">    <span class="keyword">int</span> timesOf3 = number / <span class="number">3</span>;</span><br><span class="line">    <span class="keyword">int</span> yushu = number - timesOf3 * <span class="number">3</span>;</span><br><span class="line">    <span class="keyword">if</span> (yushu == <span class="number">0</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">return</span> (<span class="keyword">int</span>)Math.Pow(<span class="number">3</span>, timesOf3);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (yushu == <span class="number">1</span>)</span><br><span class="line">    &#123;</span><br><span class="line">        timesOf3--;</span><br><span class="line">        <span class="keyword">return</span> (<span class="keyword">int</span>)Math.Pow(<span class="number">3</span>, timesOf3) * <span class="number">4</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> (<span class="keyword">int</span>)Math.Pow(<span class="number">3</span>, timesOf3) * <span class="number">2</span>;</span><br><span class="line">       </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


      
    </div>
    <footer class="article-footer">
      <a data-url="http://dontworryneedmoney.gitee.io/guolianbo/2020/04/25/dontworry/Algorithm/%E5%89%AA%E7%BB%B3%E5%AD%90%E7%AE%97%E6%B3%95/" data-id="ckb6hw6qp0000jcn28mrodt3y" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/guolianbo/tags/%E6%9D%82%E9%A1%B9/" rel="tag">杂项</a></li></ul>

    </footer>
  </div>
  
</article>


  
    <article id="ejs-dontworry/杂项/matrix4x4打印方法" class="article article-type-ejs" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/guolianbo/2020/04/25/dontworry/%E6%9D%82%E9%A1%B9/matrix4x4%E6%89%93%E5%8D%B0%E6%96%B9%E6%B3%95/" class="article-date">
  <time datetime="2020-04-25T06:04:17.000Z" itemprop="datePublished">2020-04-25</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 itemprop="name">
      <a class="article-title" href="/guolianbo/2020/04/25/dontworry/%E6%9D%82%E9%A1%B9/matrix4x4%E6%89%93%E5%8D%B0%E6%96%B9%E6%B3%95/">matrix4x4打印方法</a>
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h2><figure class="highlight csharp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">string</span> <span class="title">formatMat</span>(<span class="params">Matrix4x4 matrix</span>)</span></span><br><span class="line"><span class="function"></span>    &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">string</span>.Format(</span><br><span class="line">            <span class="string">"&#123;0:F5&#125;\t&#123;1:F5&#125;\t&#123;2:F5&#125;\t&#123;3:F5&#125;\n&#123;4:F5&#125;\t&#123;5:F5&#125;\t&#123;6:F5&#125;\t&#123;7:F5&#125;\n&#123;8:F5&#125;\t&#123;9:F5&#125;\t&#123;10:F5&#125;\t&#123;11:F5&#125;\n&#123;12:F5&#125;\t&#123;13:F5&#125;\t&#123;14:F5&#125;\t&#123;15:F5&#125;\n"</span>,</span><br><span class="line">            <span class="keyword">new</span> <span class="keyword">object</span>[]</span><br><span class="line">            &#123;</span><br><span class="line">                matrix.m00,</span><br><span class="line">                matrix.m01,</span><br><span class="line">                matrix.m02,</span><br><span class="line">                matrix.m03,</span><br><span class="line">                matrix.m10,</span><br><span class="line">                matrix.m11,</span><br><span class="line">                matrix.m12,</span><br><span class="line">                matrix.m13,</span><br><span class="line">                matrix.m20,</span><br><span class="line">                matrix.m21,</span><br><span class="line">                matrix.m22,</span><br><span class="line">                matrix.m23,</span><br><span class="line">                matrix.m30,</span><br><span class="line">                matrix.m31,</span><br><span class="line">                matrix.m32,</span><br><span class="line">                matrix.m33</span><br><span class="line">            &#125;);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>


      
    </div>
    <footer class="article-footer">
      <a data-url="http://dontworryneedmoney.gitee.io/guolianbo/2020/04/25/dontworry/%E6%9D%82%E9%A1%B9/matrix4x4%E6%89%93%E5%8D%B0%E6%96%B9%E6%B3%95/" data-id="ckb6hw6rx001rjcn23sa154ol" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/guolianbo/tags/%E6%9D%82%E9%A1%B9/" rel="tag">杂项</a></li></ul>

    </footer>
  </div>
  
</article>


  


  <nav id="page-nav">
    
    <a class="extend prev" rel="prev" href="/guolianbo/">&amp;laquo; Prev</a><a class="page-number" href="/guolianbo/">1</a><span class="page-number current">2</span><a class="page-number" href="/guolianbo/page/3/">3</a><a class="page-number" href="/guolianbo/page/4/">4</a><span class="space">&hellip;</span><a class="page-number" href="/guolianbo/page/9/">9</a><a class="extend next" rel="next" href="/guolianbo/page/3/">Next &amp;raquo;</a>
  </nav>

</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Categories</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/guolianbo/categories/%E9%9A%8F%E7%AC%94/">随笔</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list" itemprop="keywords"><li class="tag-list-item"><a class="tag-list-link" href="/guolianbo/tags/Algorithm/" rel="tag">Algorithm</a></li><li class="tag-list-item"><a class="tag-list-link" href="/guolianbo/tags/Unity3d/" rel="tag">Unity3d</a></li><li class="tag-list-item"><a class="tag-list-link" href="/guolianbo/tags/%E6%9D%82%E9%A1%B9/" rel="tag">杂项</a></li><li class="tag-list-item"><a class="tag-list-link" href="/guolianbo/tags/%E6%B6%88%E9%98%B2/" rel="tag">消防</a></li><li class="tag-list-item"><a class="tag-list-link" href="/guolianbo/tags/%E7%94%B3%E8%AE%BA/" rel="tag">申论</a></li><li class="tag-list-item"><a class="tag-list-link" href="/guolianbo/tags/%E9%9A%8F%E7%AC%94/" rel="tag">随笔</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/guolianbo/tags/Algorithm/" style="font-size: 14px;">Algorithm</a> <a href="/guolianbo/tags/Unity3d/" style="font-size: 12px;">Unity3d</a> <a href="/guolianbo/tags/%E6%9D%82%E9%A1%B9/" style="font-size: 18px;">杂项</a> <a href="/guolianbo/tags/%E6%B6%88%E9%98%B2/" style="font-size: 16px;">消防</a> <a href="/guolianbo/tags/%E7%94%B3%E8%AE%BA/" style="font-size: 10px;">申论</a> <a href="/guolianbo/tags/%E9%9A%8F%E7%AC%94/" style="font-size: 20px;">随笔</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2020/06/">June 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2020/05/">May 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2020/04/">April 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2020/03/">March 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2020/02/">February 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2020/01/">January 2020</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2019/11/">November 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2019/09/">September 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2019/08/">August 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2019/07/">July 2019</a></li><li class="archive-list-item"><a class="archive-list-link" href="/guolianbo/archives/2019/06/">June 2019</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/guolianbo/2020/06/08/dontworry/Unity3d/IObserver%E5%92%8CIObservable%E5%92%8CIDisposable/">IObserver和IObservable和IDisposable</a>
          </li>
        
          <li>
            <a href="/guolianbo/2020/05/28/dontworry/%E6%9D%82%E9%A1%B9/javascript%E7%9A%84%E6%9C%AC%E8%B4%A8/">javascript的本质</a>
          </li>
        
          <li>
            <a href="/guolianbo/2020/05/27/dontworry/%E6%9D%82%E9%A1%B9/%E5%8D%8F%E7%A8%8B%E7%9A%84%E5%8E%9F%E7%90%86/">协程的原理</a>
          </li>
        
          <li>
            <a href="/guolianbo/2020/05/20/dontworry/%E6%9D%82%E9%A1%B9/linux%E4%B8%8B%E7%9A%84nginx%E7%9A%84%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C/">linux下的nginx的基本操作</a>
          </li>
        
          <li>
            <a href="/guolianbo/2020/05/15/dontworry/%E6%9D%82%E9%A1%B9/%E5%B8%B8%E7%94%A8%E5%87%BD%E6%95%B0%E5%8A%A8%E8%AF%8D/">常用函数动词</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2020 worry dont<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/guolianbo/" class="mobile-nav-link">Home</a>
  
    <a href="/guolianbo/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>


  
<link rel="stylesheet" href="/guolianbo/fancybox/jquery.fancybox.css">

  
<script src="/guolianbo/fancybox/jquery.fancybox.pack.js"></script>




<script src="/guolianbo/js/script.js"></script>




  </div>
</body>
</html>